import time

import yaml

from project_page.base_page import BasePage


class WeworkPage(BasePage):

    _Base_url = "https://work.weixin.qq.com/wework_admin/frame#index"
    # 企业微信的公共业务的封装
    def login(self):
        self.driver.get(self._Base_url)
        cookies = yaml.safe_load(open("../data/cookie.yaml"))
        # 修复：检查文件是否存在且内容有效
        try:
            with open("../data/cookie.yaml", "r") as f:
                cookies = yaml.safe_load(f)
                if cookies is None:
                    self.fail("cookie.yaml 文件内容为空或格式错误")
        except FileNotFoundError:
            self.fail("cookie.yaml 文件不存在，请先运行 test_get_cookies")

        # 添加 cookie
        for c in cookies:
            self.driver.add_cookie(c)
        time.sleep(3)
        # 刷新页面验证登录状态
        self.driver.get(self._Base_url)

